package com.mall.util;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author hql
 *
 */
public class JavaUtil {

	

	/**
	 * 去掉字符串空格
	 * @param s
	 * @return
	 */
	public static String toTrim(String s){
		if(s==null)
			return "";
		return s.trim();
	}
	
	/**
	 * 处理查询条件
	 * @param filters
	 * @return
	 */

	public static String getCriterion(String filters){
		String criterion = "";
		if(filters!=null && !filters.equals("")){			
			JSONObject fjson = JSONObject.fromObject(filters);				//解析filters,把fileters转换为json对象
			JSONArray jarray = fjson.getJSONArray("rules");					//获得rules数组
			String groupOp   = (String) fjson.get("groupOp");				//匹配规则and、or
			int i = 0;			
			for(Object o:jarray){
				JSONObject jobject = (JSONObject) o;
				String field = jobject.getString("field");					//列名
				String op    = jobject.getString("op");						//操作符
				String data  = jobject.getString("data").trim();			//数据
				int type = 0;
				if(!data.equals("")){
//					if(field.endsWith("date") || field.endsWith("DATE")){
//						data = "to_date('"+data+"','yyyy-mm-dd')";			//日期查询时，进行转换
//						type = 1;
//					}
					criterion = criterion +field+JavaUtil.toOperator(op, data, type);	//拼接查询条件
					if(i<jarray.size()-1){
						criterion+=" "+groupOp+" ";
						i++;
					}	
				}else {
					criterion = "";
				}	
							
			}
			if(!criterion.equals("")){
				if(groupOp.equals("or")){
					criterion = "("+criterion+")";
				}
				criterion +=" and 1=1";
			}			
		}
		return criterion;
	}
	
	/**
	 * 转换查询操作符
	 * @param op,data
	 * @return String
	 */
	public static String toOperator(String op,String data,int type){
		String Operator = "";
		if(op!=null && !op.equals("") && data!=null && !data.equals("")){
			if(type==0){
				if(op.equals("eq")){
					Operator = "="+"'"+data+"'";
				}else if(op.equals("ne")){
					Operator = "<>"+"'"+data+"'";
				}else if(op.equals("lt")){
					Operator = "<"+"'"+data+"'";
				}else if(op.equals("le")){
					Operator = "<="+"'"+data+"'";
				}else if(op.equals("gt")){
					Operator = ">"+"'"+data+"'";
				}else if(op.equals("ge")){
					Operator = ">="+"'"+data+"'";
				}else if(op.equals("bw")){
					Operator = " like "+"'"+data+"%'";
				}else if(op.equals("bn")){
					Operator = " not like "+"'"+data+"%'";
				}else if(op.equals("in")){
					String[] datas = data.split(",");
					String value = "";
					for(int i=0;i<datas.length;i++){
						if(datas[0].equals(""))
							break;
						if(i!=datas.length-1){
							value = value + "'"+datas[i]+"',";
						}else{
							value = value + "'"+datas[i]+"'";
						}
					}
					Operator = " in "+"("+value+")";
				}else if(op.equals("ni")){
					Operator = "<>"+"'"+data+"'";
				}else if(op.equals("ew")){
					Operator = " like "+"'%"+data+"'";
				}else if(op.equals("en")){
					Operator = " not like "+"'%"+data+"'";
				}else if(op.equals("cn")){
					Operator = " like "+"'%"+data+"%'";
				}else if(op.equals("nc")){
					Operator = " not like "+"'%"+data+"%'";
				}
			}//else if(type == 1){
//				if(op.equals("eq")){
//					Operator = "="+data;
//				}else if(op.equals("ne")){
//					Operator = "<>"+data;
//				}else if(op.equals("lt")){
//					Operator = "<"+data;
//				}else if(op.equals("le")){
//					Operator = "<="+data;
//				}else if(op.equals("gt")){
//					Operator = ">"+data;
//				}else if(op.equals("ge")){
//					Operator = ">="+data;	
//				}
//			}
			
		}
		return Operator;
	}
	
	//去除数组中重复的记录   
	public static String[] array_unique(String[] a) {   
	    // array_unique   
	    List<String> list = new LinkedList<String>();   
	    for(int i = 0; i < a.length; i++) {   
	        if(!list.contains(a[i])) {   
	            list.add(a[i]);   
	        }   
	    }   
	    return (String[])list.toArray(new String[list.size()]);   
	}
	
	/**
	 * 处理捕获异常
	 * @param e
	 * @return
	 */
	public static String getSQLException(Exception e){
		String ecp = "";
		String regex = "ora.*\n";
		Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(e.getMessage());
		while(matcher.find(1)){
			ecp = matcher.group();
			ecp = ecp.substring(ecp.indexOf(' '),ecp.indexOf('\n'));
			break;
		}
		
		return ecp;
	}
	
	public static void main(String[] args){
		
	}
	
}
